package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import casa.depesos.app.com.activity.ChangePwdActivity;
import casa.depesos.app.com.activity.LoginActivity;
import casa.depesos.app.com.databinding.FragmentForgetCodeBinding;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.data.CodeType;
import casa.depesos.app.com.http.req.CheckCodeReq;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;
import casa.depesos.app.com.view.code.VerificationCodeInputView;

public class ForgetCodeFragment extends SmsBaseFragment {
    FragmentForgetCodeBinding binding;
    String phone;
    boolean hasRight;

    public void setParam(String phone, boolean hasRight) {
        this.phone = phone;
        this.hasRight = hasRight;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Report.show("ACTION_FF_004");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentForgetCodeBinding.inflate(inflater, container, false);
        binding.top.top.setPadding(0, Utils.getStatusBarHeight(getActivity()), 0, 0);
        binding.top.title.setText("Olvidé mi contraseña");
        binding.tip.setText(String.format("Hemos enviado un código por SMS al número telefónico %s. Ingrésalo para continuar", phone));

        binding.top.back.setOnClickListener(v -> {
            if (getActivity() instanceof LoginActivity) {
                ((LoginActivity) getActivity()).removeFragment(this);
            } else {
                getActivity().finish();
            }
        });
        binding.phone.setOnInputListener(new VerificationCodeInputView.OnInputListener() {
            @Override
            public void onComplete(String code) {
                next(code);
            }

            @Override
            public void onInput() {

            }
        });
        binding.retry.setOnClickListener(v -> {
            getCode();
        });
        if (hasRight) {
            getCode();
        }

        return binding.getRoot();
    }

    private void next(String code) {
        showLoading();
        new CheckCodeReq(phone, code, CodeType.FORGET).req(new ReqCallback<GetCodeBean>() {
            @Override
            public void callback(boolean success, GetCodeBean bean) {
                hideLoading();
                if (Constant.white || success && bean.isValid()) {
                    if (getActivity() instanceof LoginActivity) {
                        ((LoginActivity) getActivity()).toNewPwd(phone, code, 2);
                        ((LoginActivity) getActivity()).removeFragment(ForgetCodeFragment.this);
                    } else if (getActivity() instanceof ChangePwdActivity) {
                        ((ChangePwdActivity) getActivity()).toNewPwd(phone, code, 1);
                    }
                }
            }
        });
    }

    @Override
    protected String getPhone() {
        return phone;
    }

    @Override
    protected CodeType getCodeType() {
        return CodeType.FORGET;
    }

    @Override
    protected TextView getCodeTv() {
        return binding.retry;
    }

    @Override
    protected void countDown() {
        super.countDown();
        binding.retry.setTextColor(0x4d000000);
    }

    @Override
    protected void timeOut() {
        super.timeOut();
        binding.retry.setTextColor(0xff5C72FF);
    }
}
